package com.bolehui.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bolehui.base.result.Result;
import com.bolehui.base.utils.SecurityUtil;
import com.bolehui.domain.dto.ResumeDTO;
import com.bolehui.domain.po.ResumePO;
import com.bolehui.domain.po.experience.EducationExperiencePO;
import com.bolehui.domain.po.experience.JobExpectationPO;
import com.bolehui.domain.po.experience.ProjectExperiencePO;
import com.bolehui.domain.po.experience.WorkExperiencePO;
import com.bolehui.domain.vo.ResumeVO;
import com.bolehui.mapper.ResumeMapper;
import com.bolehui.service.ResumeService;
import com.bolehui.service.experience.EducationExperienceService;
import com.bolehui.service.experience.JobExpectationService;
import com.bolehui.service.experience.ProjectExperienceService;
import com.bolehui.service.experience.WorkExperienceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author chendongjie     email:chendongjie@gz.iscas.ac.cn
 * @version 1.0
 * @ClassName ResumeServiceImpl
 * @Description
 * @createTime 2024/12/16 14:41
 * Copyright (C) 2021-2022 CASEEDER, All Rights Reserved.
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 */
@Service
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, ResumePO> implements ResumeService {

    @Autowired
    private ResumeMapper resumeMapper;
    @Autowired
    private EducationExperienceService educationExperienceService;
    @Autowired
    private WorkExperienceService workExperienceService;
    @Autowired
    private ProjectExperienceService projectExperienceService;
    @Autowired
    private JobExpectationService jobExpectationService;


    @Override
    public Result save(ResumeDTO dto) {
        ResumePO resumePO = convert(dto);
        Long userId = SecurityUtil.getCurrentUserId();
        resumePO.setUserId(userId);
        return Result.success(resumeMapper.insert(resumePO));
    }

    @Override
    public Result update(ResumeDTO dto) {
        ResumePO resumePO = convert(dto);
        return Result.success(resumeMapper.updateById(resumePO));
    }

    /**
     * 查询在线简历
     * @param userId
     * @return
     */
    @Override
    public Result<ResumeVO> get(Long userId) {
        ResumePO resumePO = resumeMapper.selectOne(new QueryWrapper<ResumePO>().eq("user_id", userId));
        if(Objects.isNull(resumePO)){
            return Result.fail("简历不存在");
        }
        ResumeVO resumeVO = new ResumeVO();
        BeanUtils.copyProperties(resumePO, resumeVO);

        List<EducationExperiencePO> educationExperiencePOList = educationExperienceService.list(new QueryWrapper<EducationExperiencePO>().eq("user_id", userId));
        List<WorkExperiencePO> workExperiencePOList = workExperienceService.list(new QueryWrapper<WorkExperiencePO>().eq("user_id", userId));
        List<ProjectExperiencePO> projectExperiencePOList = projectExperienceService.list(new QueryWrapper<ProjectExperiencePO>().eq("user_id", userId));
        List<JobExpectationPO> jobExpectationPOList = jobExpectationService.list(new QueryWrapper<JobExpectationPO>().eq("user_id", userId));

        // 求职期望、工作/实习经历、项目经历和教育经历
        resumeVO.setEducationExperienceList(educationExperiencePOList == null ? Collections.emptyList() : educationExperiencePOList);
        resumeVO.setWorkExperienceList(workExperiencePOList== null ? Collections.emptyList() : workExperiencePOList);
        resumeVO.setProjectExperienceList(projectExperiencePOList== null ? Collections.emptyList() : projectExperiencePOList);
        resumeVO.setJobExpectationList(jobExpectationPOList== null ? Collections.emptyList() : jobExpectationPOList);

        return Result.success(resumeVO);
    }



    /**
     * 实体转换
     * @param dto
     * @return
     */
    private ResumePO convert(ResumeDTO dto) {
        ResumePO resumePO = new ResumePO();
        BeanUtils.copyProperties(dto, resumePO);
        return resumePO;
    }
}
